package com.shu.controller;

import com.google.common.collect.Maps;
import com.shu.exception.UserNotExistException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.Map;

/**
 * @author shujinhao
 */
@ControllerAdvice //处理所有controller抛出的异常
public class ControllerExceptionHandler {


    @ExceptionHandler(UserNotExistException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) //服务器内部错误
    public Map<String ,Object> handlerUserNotExistException(UserNotExistException ex){

        Map<String,Object> result = Maps.newHashMap();
        result.put("id",ex.getId());
        result.put("message",ex.getMessage());
        return result;
    }


}
